var util = require('../../utils/util.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },
  pageIndex: 1,
  pageSize: 100,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    const userId = app.globalData.userid
    /**
     * 查询全部订单
     */
    wx.request({
      url: app.globalData.url + 'getOrderAll',
      data: {
        userId: userId,
        pageSize: that.pageSize,
        pageIndex: that.pageIndex,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        if (res.data.resultMsg == '没有查到订单数据信息'){
          console.log(res.data.resultMsg)
        }else{
          that.setData({
            order: res.data.object.orderList
          })
          goodsList: res.data.object.orderList.filter(orderType => {
            for (const goods of res.data.object.orderList) {
              return goods.orderType == 2
              console.log(goods)
            }
            console.log(goodsList)
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  onShow:function(){
    const that = this;
    const userId = app.globalData.userid
    wx.request({
      url: app.globalData.url + 'getOrderAll',
      data: {
        userId: userId,
        pageSize: that.pageSize,
        pageIndex: that.pageIndex,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        if (res.data.resultMsg == '没有查到订单数据信息') {
          console.log(res.data.resultMsg)
        } else {
          that.setData({
            order: res.data.object.orderList
          })
          goodsList: res.data.object.orderList.filter(orderType => {
            for (const goods of res.data.object.orderList) {
              return goods.orderType == 2
              console.log(goods)
            }
            console.log(goodsList)
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 商城----状态按钮
   */
  orderButton: function (e) {
    if (!this.data.buttonClicked) {
      const shippingStatus = e.currentTarget.dataset.shippingstatus
      const payStatus = e.currentTarget.dataset.paystatus
      console.log(shippingStatus + ',' + payStatus)
      util.buttonClicked(this);
      if (payStatus == 2 && shippingStatus == 0 || shippingStatus == 3) {
        wx.showToast({
          title: '已提醒卖家发货',
          icon: 'success',
          duration: 1000,
          mask: true,
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      } else if (shippingStatus == 1) {
        //物流
        // wx.navigateTo({
        //   url: '',
        //   success: function(res) {},
        //   fail: function(res) {},
        //   complete: function(res) {},
        // })
      } else if (shippingStatus == 2) {
        wx.navigateTo({
          url: '../orderConfirm/orderConfirm?shippingStatus=' + 2,
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      } else if (payStatus == 0) {
        const goods = [e.currentTarget.dataset.goods]
        const list = JSON.stringify(goods)
        app.globalData.goodsList = list
        wx.navigateTo({
          url: '../orderPay/orderPay',
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      }
    }
  },
  mineOrderItem:function(e){
    console.log(e)
    const hrefNum = e.currentTarget.dataset.num

    if (hrefNum == 0) {
      wx.navigateTo({
        url: '../orderSon/orderSon?hrefNum=' + hrefNum,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    } else if (hrefNum == 1){
      wx.navigateTo({
        url: '../orderSon/orderSon?hrefNum=' + hrefNum,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    } else if (hrefNum == 2) {
      wx.navigateTo({
        url: '../orderSon/orderSon?hrefNum=' + hrefNum,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    } else if (hrefNum == 3) {
      wx.navigateTo({
        url: '../orderSon/orderSon?hrefNum=' + hrefNum,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    } else if (hrefNum == 4) {
      wx.navigateTo({
        url: '../orderSon/orderSon?hrefNum=' + hrefNum,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    } else if (hrefNum == 5) {
      wx.navigateTo({
        url: '../orderSon/orderSon?hrefNum=' + hrefNum,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
  orderDetail:function(){
    wx.navigateTo({
      url: '../orderDetail/orderDetail',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 食堂按钮
   */
  againOrder:function(e){
    const payStatus = e.currentTarget.dataset.paystatus
    const shippingStatus = e.currentTarget.dataset.shippingstatus
    if(payStatus == 0 || shippingStatus == 0){
      //支付
      const goods = [e.currentTarget.dataset.goods]
      const list = JSON.stringify(goods)
      wx.navigateTo({
        url: '../shouye/makeorder/makeorder?list=' + list,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
    }else{
      //再来一单
        wx.switchTab({
        url: '../shouye/index/index',
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
    }
  },
  /**
   * 商品订单详情
   */
  goodsDetail: function (e) {
    const orderType = e.currentTarget.dataset.ordertype
    const payStatus = e.currentTarget.dataset.paystatus
    const goodsId = e.currentTarget.dataset.goodsid
    const orderId = e.currentTarget.dataset.orderid

    if(orderType == 1){
      wx.navigateTo({
        url: '../mine/order/detail/detail?orderId=' + orderId,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }else if(orderType == 2){
      if (payStatus == 0) {
        wx.navigateTo({
          url: '../commodityDetail/commodityDetail?goodsId=' + goodsId,
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      } else if (payStatus == 2) {
        wx.navigateTo({
          url: '../orderDetail/orderDetail?orderId=' + orderId,
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      }
    }
  },
  /**
    * 页面上拉触底事件的处理函数
    */
  onReachBottom: function () {
    var that = this;
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })
    console.log(that.pageSize + ',' + that.pageIndex)
    // 页数+1
    // page = page + 1;
    // wx.request({
    //   url: app.globalData.url + 'getOrderAll',
    //   data: {
    //     userId: userId,
    //     pageSize: that.pageSize,
    //     pageIndex: that.pageIndex,
    //   },
    //   header: {
    //     'content-type': 'application/json' // 默认值
    //   },
    //   method: 'GET',
    //   success: function (res) {
    //     if (res.data.resultMsg == '没有查到订单数据信息') {
    //       console.log(res.data.resultMsg)
    //     } else {
    //       that.setData({
    //         order: res.data.object.orderList
    //       })
    //       goodsList: res.data.object.orderList.filter(orderType => {
    //         for (const goods of res.data.object.orderList) {
    //           return goods.orderType == 2
    //           console.log(goods)
    //         }
    //         console.log(goodsList)
    //       })
    //     }
    //   },
    //   fail: function (res) { },
    //   complete: function (res) { },
    // })
        // 隐藏加载框
        wx.hideLoading();
    //   }
    // })
  },
})